

use crate::cblash_h;
use common::complex::complex::Complex;

/*
Function
cblas_cdotc_sub
Calculates the dot product of the complex conjugate of a single-precision complex vector with a second single-precision complex vector.
https://developer.apple.com/documentation/accelerate/1513306-cblas_cdotc_sub?changes=__5&language=objc

Parameters
N
Number of elements in vectors X and Y.

X
Vector X.

incX
Stride within X. For example, if incX is 7, every 7th element is used.

Y
Vector Y.

incY
Stride within Y. For example, if incY is 7, every 7th element is used.

dotc
The result vector.

Discussion
Computes conjg(X) * Y.
 */



pub(crate) fn cblas_cdotc(
    N: i32,
    X: &Vec<Complex>,
    incX: i32,
    Y: &Vec<Complex>,
    incY: i32,
    conj_sign: f64,
) -> Complex {
    // #include "source_dot_c.h"
    if N < 1 {
        panic!("cblas_cdotc: N < 1");
    }

    let len_x = X.len();
    let len_y = Y.len();

    let ix = cblash_h::offset(N as usize, incX);
    let iy = cblash_h::offset(N as usize, incY);

    if len_x < ix.len_mustbe {
        panic!("cblas_caxpy: X 长度 {len_x} < 应该的长度 {}", ix.len_mustbe);
    }
    if len_y < iy.len_mustbe {
        panic!("cblas_caxpy: Y 长度 {len_y} < 应该的长度 {}", iy.len_mustbe);
    }

    let mut r_real = 0.0;
    let mut r_imag = 0.0;

    for idx in 0..ix.indices.len() {
        let item_x = X.get(ix.indices[idx]).expect("Unreachable");
        let item_y = Y.get(iy.indices[idx]).expect("Unreachable");
        let x_real = item_x.real;
        let x_imag = item_x.imag;
        let y_real = item_y.real;
        let y_imag = item_y.imag;

        r_real += x_real * y_real - conj_sign * x_imag * y_imag;
        r_imag += x_real * y_imag + conj_sign * x_imag * y_real;
    }
    Complex { real: r_real, imag: r_imag }
}



pub fn cblas_cdotc_sub(
    N: i32,
    X: &Vec<Complex>,
    incX: i32,
    Y: &Vec<Complex>,
    incY: i32,

) -> Complex {
    const CONJ_SIGN: f64 = -1.0;
    cblas_cdotc(N, X, incX, Y, incY, CONJ_SIGN)
}